﻿using Common;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace WebGrabber.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            string domain = Request.Url.Authority.ToLower();
            if (listManage.Count(m => m["domain"].ToString() == domain )> 0)
            {
                Dictionary<string, string> dic = listManage.FirstOrDefault(m => m["domain"].ToString() == domain);
                ViewBag.targetUrl = dic["page"].ToString();
            }
            return View();
        }

        private List<Dictionary<string, string>> listManage
        {
            get
            {
                List<Dictionary<string, string>> list = CacheUtil.Get<List<Dictionary<string, string>>>("manage");
                if (list == null)
                {
                    string confgStr = FileUtil.Read("~/manage.json");
                    list = new JavaScriptSerializer().Deserialize<List<Dictionary<string, string>>>(confgStr);
                    CacheUtil.Set("manage", list);
                }
                return list;
            }
        }
    }
}